﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exhibition.Dao.ViewModel;
using AutoMapper;
namespace Exhibition.Dao
{

    public class ColorTypeDao
    {


        public ColorTypeDao()
        {

            Mapper.CreateMap<E_ColorType, ColorTypeModel>();
        }
        public List<E_ColorType> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                recordcount = edm.E_ColorType.Count();

                return edm.E_ColorType.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public ColorTypeModel GetOneById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ColorType.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<E_ColorType, ColorTypeModel>(query);
            }

        }


        public int Save(ColorTypeModel obj)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ColorType.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new E_ColorType();

                    edm.AddToE_ColorType(query);

                }


                query.CanOrderColor = obj.CanOrderColor;

                query.OrderColor = obj.OrderColor;

                query.PayColor = obj.PayColor;

                query.OtherColor = obj.OtherColor;

                query.ColorTypeName = obj.ColorTypeName;
                query.CreateUserId = obj.CreateUserId;
                query.FColor = obj.FColor;

                edm.SaveChanges();

                return query.Id;


            }

        }
        /// <summary>
        /// 获取配色方案列表
        /// </summary>
        /// <returns></returns>
        public List<ColorTypeModel> GetColorList()
        {
            using (ExhibitionEntities edm = new ExhibitionEntities())
            {
                return Mapper.Map<List<E_ColorType>, List<ColorTypeModel>>(edm.E_ColorType.ToList());
            }
        }

        public int DelById(int id)
        {


            using (ExhibitionEntities edm = new ExhibitionEntities())
            {

                var query = edm.E_ColorType.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

